package com.blb.exam.controller;


import com.blb.exam.common.controller.BaseServlet;
import com.blb.exam.common.entity.PageBean;
import com.blb.exam.common.entity.Quetion;
import com.blb.exam.common.entity.QuetionSet;
import com.blb.exam.common.entity.Subject;
import com.blb.exam.common.util.SpringUtils;
import com.blb.exam.service.QuetionService;
import com.blb.exam.service.QuetionSetService;
import com.blb.exam.service.SubjectService;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@MultipartConfig
@WebServlet("/quetion.do")
public class QuetionServlet extends BaseServlet {

    //从spring容器中获得service对象
    private QuetionService quetionService = SpringUtils.getBean(QuetionService.class);

    private QuetionSetService quetionSetService = SpringUtils.getBean(QuetionSetService.class);

    public static final String UPLOAD_DIR = "D:\\images\\";

    private Quetion upload(HttpServletRequest req) throws IOException, ServletException {
        Part stuPicture = req.getPart("image");
        String fileName = stuPicture.getSubmittedFileName();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID().toString().replace("-","") + suffix;
        stuPicture.write(UPLOAD_DIR + fileName);
        int sId = 0;
        if(req.getParameter("id") != null){
            sId = Integer.parseInt(req.getParameter("id"));
        }
        Quetion quetion = new Quetion();
        quetion.setId(sId);
        quetion.setContent(req.getParameter("content"));
        quetion.setDifficulty(Integer.valueOf(req.getParameter("difficulty")));
        quetion.setOptionA(req.getParameter("optionA"));
        quetion.setOptionB(req.getParameter("optionB"));
        quetion.setOptionC(req.getParameter("optionC"));
        quetion.setOptionD(req.getParameter("optionD"));
        quetion.setAnswer(req.getParameter("answer"));
        quetion.setImage(fileName);
        quetion.setQuetionSetId(Integer.valueOf(req.getParameter("quetionSetId")));
        return quetion;
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Quetion quetion = upload(req);
        //添加到数据库
        int rows = quetionService.add(quetion);
        if (rows > 0) {
            //跳转到查询学生的servlet
            resp.sendRedirect("quetion.do?m=findPage");
        }
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Quetion quetion = upload(req);
        //添加到数据库
        int rows = quetionService.update(quetion);
        if (rows > 0) {
            //跳转到查询学生的servlet
            resp.sendRedirect("quetion.do?m=findPage");
        }
    }
    
    public void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得姓名参数
        String title = req.getParameter("content");
        //模糊查询
        PageBean<Quetion> page = quetionService.findPageLike(title,1);
        //保存数据，跳转
        req.setAttribute("page",page);
        req.getRequestDispatcher("/quetion.jsp").forward(req,resp);
    }

    public void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuId = req.getParameter("id");
        //查询学生
        Quetion quetion = quetionService.findById(Long.valueOf(stuId));
        //将数据传到更新页面
        req.setAttribute("quetion",quetion);
        req.getRequestDispatcher("/updateQuetion.jsp").forward(req,resp);
    }

    //填充科目到session，用于显示下拉菜单
    private void addQuetionSetsToSession(HttpSession session){
        List<QuetionSet> quetionSets = quetionSetService.findAll();
        Object sub = session.getAttribute("quetionSets");
        if(sub == null){
            session.setAttribute("quetionSets",quetionSets);
        }
    }

    //分页查询
    public void findPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String current = req.getParameter("current");
        String title = req.getParameter("content");
        title = title == null ? "" : title;
        int cur = 1;
        if(current != null){
            cur = Integer.parseInt(current);
        }
        PageBean<Quetion> page = quetionService.findPageLike(title, cur);
        //将数据传到更新页面
        req.setAttribute("page",page);
        req.getRequestDispatcher("/quetion.jsp").forward(req,resp);
        //填充科目到session，用于显示下拉菜单
        addQuetionSetsToSession(req.getSession());
    }


    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuId = req.getParameter("id");
        //添加到数据库
        int rows = quetionService.deleteById(Integer.parseInt(stuId));
        if(rows > 0){
            //跳转到查询学生的servlet
            resp.sendRedirect("quetion.do?m=findPage");
        }
    }
}
